package org.mch.comm.db.exphandler;

import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;

import org.apache.commons.lang3.StringUtils;
import org.mch.comm.exec.MsgException;
import org.mch.func.db.tool.DBInfoUtil;

/**
 * 描述：
 * 
 * @author MCH；Date：2008-10-01；<BR/>
 * 
 */
public class ExpRSHibernateVOHandler extends ExpRSSQLHandler {
	private static final long serialVersionUID = 1L;

	public ExpRSHibernateVOHandler(int dbmsType, String tableName) {
		super(dbmsType, tableName);
	}

	protected String buildClassName() {
		String tablecode = firstLetterUpperCase(getTableName().toLowerCase()) + "EO";
		String[] tmp = tablecode.split("_");
		if (tmp != null && tmp.length > 1) {
			StringBuffer tmpcode = new StringBuffer();
			for (int i = 0; i < tmp.length; i++) {
				if (i == 0) {
					tmpcode.append(tmp[i]);
				} else {
					tmpcode.append(firstLetterUpperCase(tmp[i]));
				}
			}
			return tmpcode.toString();
		}
		return tablecode;
	}

	public String firstLetterUpperCase(String str) {
		return str.substring(0, 1).toUpperCase() + str.substring(1, str.length());
	}

	public String handler(ResultSet rs) throws Exception {
		if (rs == null) {
			return "";
		}
		String table_pk = null;
		try {
			table_pk = DBInfoUtil.getTablePKField(getTableName());
		} catch (MsgException e) {
			throw new SQLException(e);
		}
		StringBuffer sb = new StringBuffer("");
		StringBuffer gs = new StringBuffer("");

		ResultSetMetaData rsm = rs.getMetaData();

		int colcount = rsm.getColumnCount();

		String tablecode = tool().firstLetterUpperCase(StringUtils.lowerCase(getTableName())) + "VO";

		sb.append("\n");
		sb.append("import javax.persistence.Column;\n");
		sb.append("import javax.persistence.Id;\n\n");
		sb.append("import javax.persistence.Entity;\n");
		sb.append("import javax.persistence.Table;\n\n");
		sb.append("import jll.base.entity.vo.BaseVO;\n\n");
		sb.append("/**\n");
		sb.append(" * ��������<BR>\n");
		sb.append(" * \n");
		sb.append(" * ����" + tablecode + ".java<BR>\n");
		sb.append(" * \n");
		sb.append(" * @author MC��Date��2008-05-01��<BR>\n");
		sb.append(" * \n");
		sb.append(" *         ˵��������ݶ�����ΪToolsResultSet�����Զ���ɣ�<BR>\n");
		sb.append(" * <BR>\n");
		sb.append(" */\n");
		sb.append("@Entity\n");
		sb.append("@Table(name = \"");
		sb.append(getTableName());
		sb.append("\")\n");
		sb.append("public class ");
		sb.append(tablecode);
		sb.append(" extends BaseVO {\n\n");
		sb.append("\t/**\n");
		sb.append("\t * \n");
		sb.append("\t */\n");
		sb.append("\t private static final long serialVersionUID = 1L;\n\n");

		for (int i = 1; i <= colcount; i++) {
			String colname = StringUtils.lowerCase(rsm.getColumnName(i));
			int coltype = rsm.getColumnType(i);
			int precision = rsm.getPrecision(i);
			int scale = rsm.getScale(i);
			String coltypestr = byColTypeGetSuperVOJavaType(coltype, precision, scale);

			sb.append("\t/**\n");
			sb.append("\t * " + colname + "��\n");
			sb.append("\t */\n");
			if (colname.equals(table_pk)) {
				sb.append("\t@Id\n");

				gs.append("\tpublic String getPk() {\n");
				gs.append("\t\treturn this." + colname + ";\n");
				gs.append("\t}\n\n");

				gs.append("\tpublic void setPk(String pk) {\n");
				gs.append("\t\tthis." + colname + " = pk;\n");
				gs.append("\t}\n\n");
			} else {
				sb.append("\t@Column\n");
			}
			sb.append("\tpublic ");
			sb.append(coltypestr);
			sb.append(" ");
			sb.append(colname);
			sb.append(";\n\n");

			gs.append("\tpublic ");
			gs.append(coltypestr);
			gs.append(" get");
			gs.append(tool().firstLetterUpperCase(colname) + "() {\n");
			gs.append("\t\treturn this." + colname + ";\n");
			gs.append("\t}\n\n");
			gs.append("\tpublic void set");
			gs.append(tool().firstLetterUpperCase(colname) + "(");
			gs.append(coltypestr);
			gs.append(" " + colname + ") {\n");
			gs.append("\t\tthis." + colname + " = " + colname + ";\n");
			gs.append("\t}\n\n");
		}

		sb.append(gs);

		sb.append("\tpublic String getPKFieldName() {\n");
		sb.append("\t\treturn \"" + table_pk + "\";\n");
		sb.append("\t}\n\n");

		sb.append("\tpublic String getTableName() {\n");
		sb.append("\t\treturn \"" + getTableName() + "\";\n");
		sb.append("\t}\n\n");

		sb.append("\tpublic String getPrimaryKey() {\n");
		sb.append("\t\treturn this." + table_pk + ";\n");
		sb.append("\t}\n\n");

		sb.append("\tpublic void setPrimaryKey(String pk) {\n");
		sb.append("\t\tthis." + table_pk + "=pk;\n");
		sb.append("\t}\n\n");

		sb.append("\tpublic String getParentPKFieldName() {\n");
		sb.append("\t\treturn null;\n");
		sb.append("\t}\n\n");

		sb.append("}\n\n\n");

		return sb.toString();
	}
}